package com.hs.srm.admin.modules.requisition.controller;

import com.hs.srm.admin.modules.requisition.entity.ProPurchRequestHeaderEntity;
import com.hs.srm.admin.modules.requisition.entity.ProPurchRequestItemEntity;
import com.hs.srm.admin.modules.requisition.service.NproPurchRequestHeaderService;
import com.hs.srm.admin.modules.requisition.service.NproPurchRequestItemService;
import com.hs.srm.admin.modules.requisition.service.ProPurchRequestHeaderService;
import com.hs.srm.admin.modules.requisition.service.ProPurchRequestItemService;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author socik
 * @date 2019/7/1 13:50
 * @email socik@qq.com
 */

@RestController
@RequestMapping("/sampleProRequest")
public class SampleProRequestController {
    @Autowired
    private NproPurchRequestHeaderService nproPurchRequestHeaderService;
    @Autowired
    private NproPurchRequestItemService nproPurchRequestItemService;
    @Autowired
    private ProPurchRequestItemService proPurchRequestItemService;
    // 注入业务层
    @Autowired
    private ProPurchRequestHeaderService proPurchRequestHeaderService;
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("purch_request_type","spro");
        //得到行项目查询条件
        String purchRequestDept = (String)params.get("purchRequestDept"); //部门
        String costCenter = (String)params.get("costCenter");//成本中心
        Map<String, Object> param = new HashMap<String,Object>();
        List<String> liststr =  new ArrayList<String>();

        PageUtils pageitem = null;
        if(purchRequestDept!=null && "".equals(purchRequestDept)){
            param.put("purchRequestDept", purchRequestDept);
            pageitem = proPurchRequestItemService.queryPage(param);
        }
        if(costCenter!=null &&"".equals(costCenter)){
            param.put("costCenter", costCenter);
            pageitem =proPurchRequestItemService.queryPage(param);
        }
        if(costCenter!=null &&"".equals(costCenter)&&purchRequestDept!=null&&"".equals(purchRequestDept)){
            param.put("costCenter", costCenter);
            param.put("purchRequestDept", purchRequestDept);
            pageitem =proPurchRequestItemService.queryPage(param);

        }
        if(pageitem!=null){
            List<ProPurchRequestItemEntity> proPurchRequestItemList =  (List<ProPurchRequestItemEntity>) pageitem.getList();
            for(ProPurchRequestItemEntity item : proPurchRequestItemList){
                liststr.add(item.getPurchRequestCode());
            }
        }
        // 查询用户列表
        PageUtils page = proPurchRequestHeaderService.queryPage(params);
        List<ProPurchRequestHeaderEntity> proPurchRequestHeaderList = (List<ProPurchRequestHeaderEntity>) page.getList();
        List<ProPurchRequestHeaderEntity> proPurchRequestHeaderListtem =new ArrayList<ProPurchRequestHeaderEntity>();
        if(liststr!=null&&liststr.size()>0){
            for(String str : liststr){
                for(ProPurchRequestHeaderEntity header: proPurchRequestHeaderList){
                    if(str.equals(header.getPurchRequestCode())){
                        proPurchRequestHeaderListtem.add(header);
                    }
                }
            }
        }
        return R.ok(page);
    }
}
